package openhaven.control.message;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.BlockingQueue;

import openhaven.control.message.RichMessage.NewWidgetMessage;
import openhaven.control.net.Message;
import openhaven.view.HavenFrame;

import org.apache.log4j.Logger;

public class RemoteMessageWorker implements Runnable
{
    private static final Logger LOG = Logger.getLogger(RemoteMessageWorker.class);
    final HavenFrame frame;
    final BlockingQueue<Message> uiMessageQueue;
    final Map<Object, Object> messageListeners = new HashMap<Object, Object>();

    public RemoteMessageWorker(final HavenFrame frame, final BlockingQueue<Message> uiMessageQueue)
    {
        this.frame = frame;
        this.uiMessageQueue = uiMessageQueue;
    }

    @Override
    public void run()
    {
        while (true)
        {
            try
            {
                final Message message = uiMessageQueue.take();
                final RichMessage richMessage = RichMessage.process(message);
                LOG.debug("thread workere received message of type:" + richMessage);
                if (richMessage instanceof RichMessage.NewWidgetMessage)
                {
                    final NewWidgetMessage msg = (NewWidgetMessage) richMessage;
                    System.out.println("create widget of type:" + msg.type + " and id: " + msg.id);
                }
            }
            catch (final InterruptedException e)
            {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }
}
